import { useUserInfoStore } from '@gis/lib/pinia/userInfo.js';
import get from 'lodash-es/get.js';

function checkDwStr(str) {
  return str && typeof str === 'string';
}

export function getCombineDW({ labels, values }) {
  if (!checkDwStr(labels) || !checkDwStr(values)) {
    return;
  }
  labels = labels.split(',');
  values = values.split(',');
  return values.reduce((res, item, index) => {
    if (!item || !labels[index]) return res;
    return res.concat([{ label: labels[index], value: item }]);
  }, []);
}
export function getSplitDW(dw, replaceFields) {
  if (!Array.isArray(dw)) {
    return {};
  }
  const fields = Object.assign({ labels: 'dwmc', values: 'dwbm' }, replaceFields);
  return {
    [fields.labels]: dw.map((item) => item.label).join(','),
    [fields.values]: dw.map((item) => item.value).join(','),
  };
}

export function getIsManager() {
  const userInfo = useUserInfoStore();
  const unitCode = get(userInfo, 'userData.unitCode');
  const accountId = get(userInfo, 'userData.accountId');
  return (
    unitCode === '86.001.001.001.001.@003.002' /* 情指中心单位码 */ ||
    accountId === 'ff8080818386f7ff01838943411a0036' /* chqga 账号 */
  );
}

export function getIsMine(cjrid) {
  const userInfo = useUserInfoStore();
  const accountId = get(userInfo, 'userData.accountId');
  return cjrid === accountId;
}

export function getIsForMe(qsdwbm) {
  if (!checkDwStr(qsdwbm)) {
    return false;
  }
  const userInfo = useUserInfoStore();
  const unitCode = get(userInfo, 'userData.unitCode');
  return qsdwbm.split(',').includes(unitCode);
}

export function getIsEnded(data) {
  const status = get(data, 'qszt');
  return ['已结案', '已撤销'].includes(status);
}

export function getIsReceived(sqdwbm) {
  if (!checkDwStr(sqdwbm)) {
    return false;
  }
  const userInfo = useUserInfoStore();
  const unitCode = get(userInfo, 'userData.unitCode');
  return sqdwbm.split(',').includes(unitCode);
}

export function getDwLength(dwStr) {
  if (!checkDwStr(dwStr)) {
    return 0;
  }
  return dwStr.split(',').filter(Boolean).length;
}
